<?php
/*======================================================================*\
|| #################################################################### ||
|| # ragtek Template Compare  1.0.0
|| # Build: 1
|| # ---------------------------------------------------------------- # ||
|| # Copyright 2011 Daniel Fatkic aka ragtek. All Rights Reserved.   # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # http://www.ragtek.org                                            # ||
|| #################################################################### ||
\*======================================================================*/


class Ragtek_TC_ControllerAdmin_Index extends XenForo_ControllerAdmin_Abstract {

    public function actionIndex() {

        $templateModel = $this->_getTemplateModel();
        $templates = $templateModel->getOutdatedTemplates();

        #die(print_r($templates));
    $viewParams = array(
        'templates' =>  $templates
    );

                return $this->responseView('', 'ragtek_templatecompare_list', $viewParams
#                    , array('containerTemplate' =>'compare_container')
        );


        return $this->actionCompare();
    }

    public function actionCompare() {

        $templateName = $this->_input->filterSingle('template', XenForo_Input::STRING);
        $templateName = 'ragtek_infopage_wrapper';
        if ($templateName == '') {
            die('kein template');
        }


        $compareModel = $this->getCompareModel();

        $templates = $compareModel->getTemplates($templateName);

        $templateOldArray = explode("\n", $templates[0]['template']);
        $templateNewArray = explode("\n",$templates[1]['template']);

        // only for bugtracking
        $file1 = 'd:/was.txt';
        $file2 = 'd:/was2.txt';

     #   $templateOldArray = explode("\n", file_get_contents($file1));
      #  $templateNewArray = explode("\n", file_get_contents($file2));

        $out = $compareModel->diff($templateOldArray, $templateNewArray, 'master', $templates[1]['styletitle']);

        $viewParams = array(
            'templatename' => $templates[1]['title'],
            'out' => $out
        );

        return $this->responseView('', 'ragtek_templatecompare_main', $viewParams
#                    , array('containerTemplate' =>'compare_container')
        );
    }

    protected function _getTemplateOrError($id) {
        return $this->getRecordOrError(
            $id, $this->_getTemplateModel(), 'getTemplateById', 'requested_template_not_found'
        );
    }

    /**
     * return XenForo_Model_Template
     */
    protected function _getTemplateModel() {
        return $this->getModelFromCache('XenForo_Model_Template');
    }

    /**
     * return Ragtek_TC_Model_TemplateCompare
     */
    protected function getCompareModel() {
        return $this->getModelFromCache('Ragtek_TC_Model_TemplateCompare');
    }
}